//封装promise请求
import {axios} from "./Interceptor";
import store from "@/store"
import {message} from 'ant-design-vue';

let timer = null;
/**
 *
 * @param urls
 * @param method 请求方法 post get delete put 等值
 * @param params 请求参数
 * @param mode   请求的传参方式 query  或者 body
 */
export const getData = (urls, method, params, mode) => {
    // const store = useStore()
    // const {ctx} = getCurrentInstance()
    store.commit("SET_LOADING", true)
    timer = setTimeout(() => {
        store.commit("SET_LOADING", false)
        // message.error("登录超时")
        clearTimeout(timer)
    }, 10000)
    let token = ""
    try {
        token = store.state.user.token
    } catch (e) {
        token = ""
    }
    let url = "/api" + urls;
    return new Promise((reject, resolve) => {
        axios({
            url: url,
            method: method,
            [mode]: params,
            headers: {
                Authorization: 'Bearer ' + token
            }
        }).then((res) => {
            clearTimeout(timer)
            store.commit("SET_LOADING", false)
            reject(res.data);
            // message.error("服务器异常")
        }).catch((err) => {
            resolve(err);
            clearTimeout(timer)
            console.log("服务器异常")
            store.commit("SET_LOADING", false)
            message.error("服务器异常")
        })
    })
}




